package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.service.impl.FeeKeyServiceImpl;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
public class FeeController {
    @Autowired
    private FeeKeyServiceImpl feeKeyService;
    @Autowired
    private FeeValueService feeValueService;

    /**
     * todo:逻辑：根据对象中id，执行保存或更新杂费名称
     *      判定执行结果，并返回
     * @param feeKey
     * @return
     */
    @Operation(summary = "保存或更新杂费名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateFeeKey(@RequestBody FeeKey feeKey) {
        boolean is = feeKeyService.saveOrUpdate(feeKey);
        if(!is){
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * todo:逻辑：根据对象中id，执行保存或更新杂费值操作
     *          判定执行结果，并返回
     * @param feeValue
     * @return
     */
    @Operation(summary = "保存或更新杂费值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateFeeValue(@RequestBody FeeValue feeValue) {
        boolean is = feeValueService.saveOrUpdate(feeValue);
        if(!is){
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * todo:逻辑；多表连接查询，所有未删除杂费名和所有未删除杂费值列表
     * todo:注意：多表查询，左外连接，子查询条件加在on条件后，不是where，否则会变成内连接的效果
     * @return
     */
    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("list")
    public Result<List<FeeKeyVo>> feeInfoList() {
        List<FeeKeyVo> list = feeKeyService.customList();
        return Result.ok(list);
    }

    /**
     * todo：逻辑：根据id，删除杂费名，
     *          根据杂费id，删除杂费值
     * todo：注意点：避免孤儿数据
     * @param feeKeyId
     * @return
     */
    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("key/deleteById")
    public Result deleteFeeKeyById(@RequestParam Long feeKeyId) {
        //根据id，删除杂费名
        feeKeyService.removeById(feeKeyId);
        //构造条件，where 字段 = feeKeyId
        LambdaQueryWrapper<FeeValue> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(FeeValue::getFeeKeyId,feeKeyId);
        //根据条件，删除杂费值
        feeValueService.remove(queryWrapper);
        //返回结果 result
        return Result.ok();
    }

    /**
     * todo:逻辑；根据id，删除杂费值
     * @param id
     * @return
     */
    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("value/deleteById")
    public Result deleteFeeValueById(@RequestParam Long id) {
        feeValueService.removeById(id);
        return Result.ok();
    }
}
